home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / xpcom / nsHashKeys.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  9KB  |  326 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is C++ hashtable templates.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Benjamin Smedberg.
  19.  * Portions created by the Initial Developer are Copyright (C) 2002
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. #ifndef nsTHashKeys_h__
  39. #define nsTHashKeys_h__
  40.  
  41. #include "nsID.h"
  42. #include "nsISupports.h"
  43. #include "nsCOMPtr.h"
  44. #include "pldhash.h"
  45. #include NEW_H
  46.  
  47. #ifdef MOZILLA_INTERNAL_API
  48. #include "nsAString.h"
  49. #include "nsString.h"
  50. #else
  51. #include "nsStringAPI.h"
  52. #endif
  53.  
  54. #include <stdlib.h>
  55. #include <string.h>
  56.  
  57. /** @file nsHashKeys.h
  58.  * standard HashKey classes for nsBaseHashtable and relatives. Each of these
  59.  * classes follows the nsTHashtable::EntryType specification
  60.  *
  61.  * Lightweight keytypes provided here:
  62.  * nsStringHashKey
  63.  * nsCStringHashKey
  64.  * nsUint32HashKey
  65.  * nsISupportsHashKey
  66.  * nsIDHashKey
  67.  * nsDepCharHashKey
  68.  */
  69.  
  70. NS_COM_GLUE PRUint32 HashString(const nsAString& aStr);
  71. NS_COM_GLUE PRUint32 HashString(const nsACString& aStr);
  72. NS_COM_GLUE PRUint32 HashCString(const char* aKey);
  73.  
  74. /**
  75.  * hashkey wrapper using nsAString KeyType
  76.  *
  77.  * @see nsTHashtable::EntryType for specification
  78.  */
  79. class nsStringHashKey : public PLDHashEntryHdr
  80. {
  81. public:
  82.   typedef const nsAString& KeyType;
  83.   typedef const nsAString* KeyTypePointer;
  84.  
  85.   nsStringHashKey(KeyTypePointer aStr) : mStr(*aStr) { }
  86.   nsStringHashKey(const nsStringHashKey& toCopy) : mStr(toCopy.mStr) { }
  87.   ~nsStringHashKey() { }
  88.  
  89.   KeyType GetKey() const { return mStr; }
  90.   KeyTypePointer GetKeyPointer() const { return &mStr; }
  91.   PRBool KeyEquals(const KeyTypePointer aKey) const
  92.   {
  93.     return mStr.Equals(*aKey);
  94.   }
  95.  
  96.   static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
  97.   static PLDHashNumber HashKey(const KeyTypePointer aKey)
  98.   {
  99.     return HashString(*aKey);
  100.   }
  101.   enum { ALLOW_MEMMOVE = PR_TRUE };
  102.  
  103. private:
  104.   const nsString mStr;
  105. };
  106.  
  107. /**
  108.  * hashkey wrapper using nsACString KeyType
  109.  *
  110.  * @see nsTHashtable::EntryType for specification
  111.  */
  112. class nsCStringHashKey : public PLDHashEntryHdr
  113. {
  114. public:
  115.   typedef const nsACString& KeyType;
  116.   typedef const nsACString* KeyTypePointer;
  117.   
  118.   nsCStringHashKey(const nsACString* aStr) : mStr(*aStr) { }
  119.   nsCStringHashKey(const nsCStringHashKey& toCopy) : mStr(toCopy.mStr) { }
  120.   ~nsCStringHashKey() { }
  121.  
  122.   KeyType GetKey() const { return mStr; }
  123.   KeyTypePointer GetKeyPointer() const { return &mStr; }
  124.  
  125.   PRBool KeyEquals(KeyTypePointer aKey) const { return mStr.Equals(*aKey); }
  126.  
  127.   static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
  128.   static PLDHashNumber HashKey(KeyTypePointer aKey)
  129.   {
  130.     return HashString(*aKey);
  131.   }
  132.   enum { ALLOW_MEMMOVE = PR_TRUE };
  133.  
  134. private:
  135.   const nsCString mStr;
  136. };
  137.  
  138. /**
  139.  * hashkey wrapper using PRUint32 KeyType
  140.  *
  141.  * @see nsTHashtable::EntryType for specification
  142.  */
  143. class nsUint32HashKey : public PLDHashEntryHdr
  144. {
  145. public:
  146.   typedef const PRUint32& KeyType;
  147.   typedef const PRUint32* KeyTypePointer;
  148.   
  149.   nsUint32HashKey(KeyTypePointer aKey) : mValue(*aKey) { }
  150.   nsUint32HashKey(const nsUint32HashKey& toCopy) : mValue(toCopy.mValue) { }
  151.   ~nsUint32HashKey() { }
  152.  
  153.   KeyType GetKey() const { return mValue; }
  154.   KeyTypePointer GetKeyPointer() const { return &mValue; }
  155.   PRBool KeyEquals(KeyTypePointer aKey) const { return *aKey == mValue; }
  156.  
  157.   static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
  158.   static PLDHashNumber HashKey(KeyTypePointer aKey) { return *aKey; }
  159.   enum { ALLOW_MEMMOVE = PR_TRUE };
  160.  
  161. private:
  162.   const PRUint32 mValue;
  163. };
  164.  
  165. /**
  166.  * hashkey wrapper using nsISupports* KeyType
  167.  *
  168.  * @see nsTHashtable::EntryType for specification
  169.  */
  170. class nsISupportsHashKey : public PLDHashEntryHdr
  171. {
  172. public:
  173.   typedef nsISupports* KeyType;
  174.   typedef const nsISupports* KeyTypePointer;
  175.  
  176.   nsISupportsHashKey(const nsISupports* key) :
  177.     mSupports(NS_CONST_CAST(nsISupports*,key)) { }
  178.   nsISupportsHashKey(const nsISupportsHashKey& toCopy) :
  179.     mSupports(toCopy.mSupports) { }
  180.   ~nsISupportsHashKey() { }
  181.  
  182.   KeyType GetKey() const { return mSupports; }
  183.   KeyTypePointer GetKeyPointer() const { return mSupports; }
  184.   
  185.   PRBool KeyEquals(KeyTypePointer aKey) const { return aKey == mSupports; }
  186.  
  187.   static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
  188.   static PLDHashNumber HashKey(KeyTypePointer aKey)
  189.   {
  190.     return NS_PTR_TO_INT32(aKey) >>2;
  191.   }
  192.   enum { ALLOW_MEMMOVE = PR_TRUE };
  193.  
  194. private:
  195.   nsCOMPtr<nsISupports> mSupports;
  196. };
  197.  
  198. /**
  199.  * hashkey wrapper using void* KeyType
  200.  *
  201.  * @see nsTHashtable::EntryType for specification
  202.  */
  203. class nsVoidPtrHashKey : public PLDHashEntryHdr
  204. {
  205. public:
  206.   typedef const void* KeyType;
  207.   typedef const void* KeyTypePointer;
  208.  
  209.   nsVoidPtrHashKey(const void* key) :
  210.     mKey(key) { }
  211.   nsVoidPtrHashKey(const nsVoidPtrHashKey& toCopy) :
  212.     mKey(toCopy.mKey) { }
  213.   ~nsVoidPtrHashKey() { }
  214.  
  215.   KeyType GetKey() const { return mKey; }
  216.   KeyTypePointer GetKeyPointer() const { return mKey; }
  217.   
  218.   PRBool KeyEquals(KeyTypePointer aKey) const { return aKey == mKey; }
  219.  
  220.   static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
  221.   static PLDHashNumber HashKey(KeyTypePointer aKey)
  222.   {
  223.     return NS_PTR_TO_INT32(aKey) >>2;
  224.   }
  225.   enum { ALLOW_MEMMOVE = PR_TRUE };
  226.  
  227. private:
  228.   const void* mKey;
  229. };
  230.  
  231. /**
  232.  * hashkey wrapper using nsID KeyType
  233.  *
  234.  * @see nsTHashtable::EntryType for specification
  235.  */
  236. class nsIDHashKey : public PLDHashEntryHdr
  237. {
  238. public:
  239.   typedef const nsID& KeyType;
  240.   typedef const nsID* KeyTypePointer;
  241.   
  242.   nsIDHashKey(const nsID* inID) : mID(*inID) { }
  243.   nsIDHashKey(const nsIDHashKey& toCopy) : mID(toCopy.mID) { }
  244.   ~nsIDHashKey() { }
  245.  
  246.   KeyType GetKey() const { return mID; }
  247.   KeyTypePointer GetKeyPointer() const { return &mID; }
  248.  
  249.   PRBool KeyEquals(KeyTypePointer aKey) const { return aKey->Equals(mID); }
  250.  
  251.   static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
  252.   static PLDHashNumber HashKey(KeyTypePointer aKey);
  253.   enum { ALLOW_MEMMOVE = PR_TRUE };
  254.  
  255. private:
  256.   const nsID mID;
  257. };
  258.  
  259. /**
  260.  * hashkey wrapper for "dependent" const char*; this class does not "own"
  261.  * its string pointer.
  262.  *
  263.  * This class must only be used if the strings have a lifetime longer than
  264.  * the hashtable they occupy. This normally occurs only for static
  265.  * strings or strings that have been arena-allocated.
  266.  *
  267.  * @see nsTHashtable::EntryType for specification
  268.  */
  269. class nsDepCharHashKey : public PLDHashEntryHdr
  270. {
  271. public:
  272.   typedef const char* KeyType;
  273.   typedef const char* KeyTypePointer;
  274.  
  275.   nsDepCharHashKey(const char* aKey) { mKey = aKey; }
  276.   nsDepCharHashKey(const nsDepCharHashKey& toCopy) { mKey = toCopy.mKey; }
  277.   ~nsDepCharHashKey() { }
  278.  
  279.   const char* GetKey() const { return mKey; }
  280.   const char* GetKeyPointer() const { return mKey; }
  281.   PRBool KeyEquals(const char* aKey) const
  282.   {
  283.     return !strcmp(mKey, aKey);
  284.   }
  285.  
  286.   static const char* KeyToPointer(const char* aKey) { return aKey; }
  287.   static PLDHashNumber HashKey(const char* aKey) { return HashCString(aKey); }
  288.   enum { ALLOW_MEMMOVE = PR_TRUE };
  289.  
  290. private:
  291.   const char* mKey;
  292. };
  293.  
  294. /**
  295.  * hashkey wrapper for const char*; at construction, this class duplicates
  296.  * a string pointed to by the pointer so that it doesn't matter whether or not
  297.  * the string lives longer than the hash table.
  298.  */
  299. class nsCharPtrHashKey : public PLDHashEntryHdr
  300. {
  301. public:
  302.   typedef const char* KeyType;
  303.   typedef const char* KeyTypePointer;
  304.  
  305.   nsCharPtrHashKey(const char* aKey) : mKey(strdup(aKey)) { }
  306.   nsCharPtrHashKey(const nsCharPtrHashKey& toCopy) : mKey(strdup(toCopy.mKey)) { }
  307.   ~nsCharPtrHashKey() { if (mKey) free(NS_CONST_CAST(char *, mKey)); }
  308.  
  309.   const char* GetKey() const { return mKey; }
  310.   const char* GetKeyPointer() const { return mKey; }
  311.   PRBool KeyEquals(KeyTypePointer aKey) const
  312.   {
  313.     return !strcmp(mKey, aKey);
  314.   }
  315.  
  316.   static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
  317.   static PLDHashNumber HashKey(KeyTypePointer aKey) { return HashCString(aKey); }
  318.  
  319.   enum { ALLOW_MEMMOVE = PR_TRUE };
  320.  
  321. private:
  322.   const char* mKey;
  323. };
  324.  
  325. #endif // nsTHashKeys_h__
  326.